package com.haredot.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.haredot.entity.Swiper;
import com.haredot.oss.properties.OssProperties;
import com.haredot.service.SwiperService;
import com.haredot.validation.Insert;
import com.haredot.validation.Update;
import com.haredot.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.lang.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Part;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/swiper")
@EnableConfigurationProperties(OssProperties.class)
public class SwiperController {

    private SwiperService swiperService;

    private OssProperties ossProperties;

    private OSS ossClient;

    @Autowired
    public SwiperController(SwiperService swiperService, @Nullable OSS ossClient, OssProperties ossProperties) {
        this.swiperService = swiperService ;
        this.ossClient = ossClient ;
        this.ossProperties = ossProperties ;
    }


    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('swiper')")
    public R<?> list() {
        return swiperService.querySwipers();
    }

    @PostMapping("/upload")
    @PreAuthorize("hasAnyAuthority('swiper:add', 'swiper:modify')")
    public R<?> upload(@RequestPart("swiper") MultipartFile part) throws Exception {

        // 获取 要存储在 磁盘上的文件名
        String filename = "swiper/" + UUID.randomUUID() + part.getOriginalFilename();
        // 构建 PutObjectRequest对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), filename, part.getInputStream());
        // 上传文件到网盘
        ossClient.putObject(putObjectRequest);
        // 返回结果
        return R.ok().data(Map.of("url", filename));
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('swiper:add')")
    public R<?> addSwiper(@RequestBody @Validated(Insert.class) Swiper swiper) {
        return swiperService.saveSwiper(swiper);

    }

    @PutMapping("/update/{id}")
    @PreAuthorize("hasAuthority('swiper:modify')")
    public R<?> updateSwiper(@PathVariable("id") Long id ,@RequestBody @Validated(Update.class) Swiper swiper) {
        Assert.isTrue(Objects.equals(id, swiper.getId()), "ID参数不正确");
        return swiperService.updateSwiper(swiper);

    }


    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('swiper:remove')")
    public R<?> removeSwiper(@PathVariable("id") Long id) {
        return swiperService.removeById(id);
    }

    @DeleteMapping()
    @PreAuthorize("hasAuthority('swiper:batch:remove')")
    public R<?> batchRemoveSwiper(@RequestBody List<Long> ids) {
        return swiperService.removeByIds(ids);
    }


    /**
     * 该接口 RSS 系统需要使用， 对外暴露即可
     */
    @GetMapping(value = "/getSwipersAndAdvert", headers = "RSS-Authorization=rss-front")
    public R<Map<String, Object>> getSwipersAndAdvert(@RequestParam("sys") String sysRemark) {
        return swiperService.querySwipersAndAdverts(sysRemark);
    }

}
